<?php
// include_once($relPath.'site_vars.php');

// -- site

function redirect_to_home() {
    global $code_url;
    redirect_to_url($code_url);
}

function link_to_css($filename) {
    global $css_url;
    return "<link type='text/css' "
                    ."rel='stylesheet' "
                    ."href='{$css_url}/{$filename}'>\n";
}

function favicon() {
    global $code_url;
    return "<link rel='shortcut icon' href='$code_url/favicon.ico'>\n";
}

function link_to_js($filename) {
    global $js_url;
    return "<script src='{$js_url}/{$filename}'></script>\n";
}

function link_to_site($text = "") {   
    global $site_abbreviation;
    if(! $text)
        $text = $site_abbreviation;
    return link_to_url(url_for_site(), $text);
}

function url_for_site() {
    global $code_url;
    return $code_url;
}

// -- registration

function url_for_registration() {
    global $registration_url;
    return $registration_url;
}

function link_to_registration($text = "") {
    if(! $text)
        $text = _("Register");
    return link_to_url(url_for_registration(), $text);
}

// -- wordcheck

function url_for_wordcheck($projectid, $pagename) {
    global $proof_url;
    return unamp("$proof_url/spellcheck.php"
                            ."?projectid=$projectid"
                            ."&pagename=$pagename");
}

// -- processtext

function url_for_processtext() {
    global $proof_url;
    return "$proof_url/processtext.php";
}

function url_for_process_page($projectid, $pagename) {
    global $proof_url;
    return unamp("$proof_url/processtext.php"
                            ."?projectid=$projectid"
                            ."&pagename=$pagename");
}

// -- project

function url_for_project($projectid) {
    global $code_url;
    return "$code_url/project.php?projectid={$projectid}";
}

function link_to_project($projectid, $prompt) {
    return link_to_url(url_for_project($projectid), $prompt);
}

function redirect_to_project($projectid) {
    redirect_to_url(url_for_project($projectid));
}

// -- differ

function url_for_differ($projectid) {
    global $proof_url;
    return "$proof_url/differ.php?projectid={$projectid}";
}

function link_to_differ($projectid, $prompt) {
    return link_to_url(url_for_differ($projectid), $prompt);
}

// -- edit project

function url_for_edit_project($projectid) {
    global $pm_url;
    return unamp("{$pm_url}/editproject.php"
                    ."?projectid={$projectid}"
                    ."&todo=edit");
}

function link_to_edit_project($projectid, $prompt, $newtab = false) {
    return link_to_url(
            url_for_edit_project($projectid), $prompt, $newtab);
}

// -- create project

function link_to_create_project($prompt = "Create project") {
    return link_to_url(url_for_create_project(), $prompt);
}

function url_for_create_project() {
    global $pm_url;
    return unamp("{$pm_url}/editproject.php"
                    ."?todo=createnew");
}

// -- delete project

function link_to_delete_project($prompt = "Delete project") {
    return link_to_url(url_for_delete_project(), $prompt);
}

function url_for_delete_project() {
    global $pm_url;
    return unamp("{$pm_url}/editproject.php"
                    ."?todo=deleteProject");
}



function redirect_to_edit_project($projectid) {
    redirect_to_url(url_for_edit_project($projectid));
}

// -- round page


function link_to_round($roundid, $prompt = "") {
    if($prompt == "")
        $prompt = "{$roundid}";
    return "<a href='".url_for_round($roundid)."'"
        ." title='".h(_("Proofreading for Round $roundid"))."'>"
        .h(_($prompt))."</a>";
}

function url_for_round($roundid) {
    global $proof_url;
    return "$proof_url/round.php?roundid=$roundid";
}

// -- task page



function link_to_task($taskid, $prompt = "") {
    if($prompt == "")
        $prompt = "{$taskid}";
    return "<a href='".url_for_task($taskid)."'"
        ." title='".h(_("Proofreading for Task $taskid"))."'>"
        .h(_($prompt))."</a>";
}

function url_for_task($taskid) {
    global $code_url;
    return "$code_url/search.php?qtask[]=$taskid";
}




// -- proof page

function link_to_proof_page($projectid, $pagename, $prompt) {
    return link_to_url(
        url_for_proof_page($projectid, $pagename), $prompt);
}

function url_for_proof_page($projectid, $pagename) {
    global $proof_url;
    return
        unamp("$proof_url/proof.php?projectid=$projectid"
                            ."&pagename=$pagename");
}

function redirect_to_proof($projectid, $pagename) {
    redirect_to_url(url_for_proof($projectid, $pagename));
}

// -- proof next

function link_to_proof_next($projectid, $prompt) {
    return link_to_url(
        url_for_proof_next($projectid), $prompt);
}

function url_for_proof_next($projectid) {
    global $proof_url;
    return "$proof_url/proof.php?projectid=$projectid";
}

function redirect_to_proof_next($projectid) {
    redirect_to_url(url_for_proof_next($projectid));
}

// -- proof

function link_to_proof($projectid, $prompt) {
    return link_to_url( url_for_proof($projectid), $prompt);
}

function url_for_proof($projectid, $pagename) {
    global $proof_url;
    return
        unamp("$proof_url/proof.php?projectid=$projectid"
                            ."&pagename=$pagename");
}

// -- smooth reading

function link_to_smooth_reading($prompt) {
    return link_to_url(url_for_smooth_reading(), $prompt);
}

function url_for_smooth_reading() {
    global $pm_url;
    return "$pm_url/smooth_reading.php";
}

// -- wdiff

function link_to_wdiff_results($projectid, $langcode, $prompt) {
    return link_to_url( 
            url_for_wdiff_results($projectid, $langcode), $prompt);
}

function url_for_wdiff_results($projectid, $langcode) {
    global $wc_url;
    return "$wc_url/wdiff.php?projectid=$projectid"
                        ."&langcode=$langcode";
}

// -- diff suggestions

function link_to_diff_suggestions($projectid, $prompt) {
    return link_to_url( 
            url_for_diff_suggestions($projectid), $prompt);
}

function url_for_diff_suggestions($projectid) {
    global $wc_url;
    return "$wc_url/scannos.php?projectid=$projectid";
}

// -- suggested words

function link_to_suggested_words(
                $projectid, $prompt, $is_new_tab = false) {
    return link_to_url(
        url_for_suggested_words($projectid), $prompt, $is_new_tab);
}

function url_for_suggested_words($projectid) {
    return url_for_word_context($projectid, "suggested");
}

// -- good words

function link_to_good_words(
                    $projectid, $prompt, $is_new_tab = false) {
    return link_to_url(url_for_good_words($projectid), $prompt,
                            $is_new_tab);
}

function url_for_good_words($projectid) {
    return url_for_word_context($projectid, "good");
}

// -- bad words

function link_to_bad_words($projectid, $prompt, $is_new_tab = false) {
    return link_to_url(url_for_bad_words($projectid), $prompt,
                            $is_new_tab);
}

function url_for_bad_words($projectid) {
    return url_for_word_context($projectid, "bad");
}

// -- wordcheck flags (word context)

function link_to_wordcheck_flags(
                $projectid, $prompt, $is_new_tab = false) {
    return link_to_url(
        url_for_wordcheck_flags($projectid), $prompt, $is_new_tab);
}

function url_for_wordcheck_flags($projectid) {
    return url_for_word_context($projectid, "flagged");
}

// -- worcheck usage

function link_to_wordcheck_usage($projectid, $prompt) {
    return link_to_url( url_for_wordcheck_usage($projectid), $prompt);
}

function url_for_wordcheck_usage($projectid) {
    global $wc_url;
    return "$wc_url/wordcheck_usage.php?projectid=$projectid";
}

// -- wordcheck stats

function link_to_wordcheck_stats($projectid, $prompt) {
    return link_to_url( url_for_wordcheck_stats($projectid), $prompt);
}

function url_for_wordcheck_stats($projectid) {
    global $wc_url;
    return "$wc_url/wordcheck_stats.php?projectid=$projectid";
}

// -- ad hoc words

function link_to_adhoc_words($projectid, $prompt) {
    return link_to_url(url_for_adhoc_words($projectid), $prompt);
}

function url_for_adhoc_words($projectid) {
    return url_for_word_context($projectid, "adhoc");
}

// -- proof frame

function url_for_proof_frame($projectid, $pagename) {
    global $proof_url;

    return unamp("{$proof_url}/proof_frame.php"
                    ."?projectid={$projectid}"
                    . ($pagename ? "&pagename={$pagename}" : ""));
}

// -- text frame std

function url_for_text_frame_std($projectid, $pagename) {
    global $proof_url;

    return unamp("{$proof_url}/text_frame_std.php"
                    ."?projectid={$projectid}"
                    ."&pagename={$pagename}");
}

function redirect_to_text_frame_std($projectid, $pagename) {
    redirect_to_url(url_for_text_frame_std($projectid, $pagename));
}

// -- text frame enh

function url_for_proof_frame_enh($page) {
    global $proof_url;

    $projectid = $page->ProjectId();
    $pagename  = $page->PageName();

    return unamp("{$proof_url}/proof_frame_enh.php"
                    ."?projectid={$projectid}"
                    ."&pagename={$pagename}");
}

function redirect_to_proof_frame_enh($page) {
    redirect_to_url(url_for_proof_frame_enh($page));
}

// -- forums

function link_to_forums($prompt = "Forums") {
    global $forums_url;
    return link_to_url($forums_url, $prompt);
}

// -- project thread

function link_to_project_thread($postid, $prompt) {
    return link_to_url(url_for_project_thread($postid), $prompt);
}

function url_for_project_thread($postid) {
    global $forums_url;
    return build_path($forums_url,
                "viewtopic.php?p={$postid}");
}

// -- private message

function url_for_private_message($username) {
    global $forums_url;
    return unamp(build_path($forums_url,
                "ucp.php?i=pm&mode=compose&un={$username}"));
}

function link_to_private_message($username, $prompt = null) {
    if(! $prompt) $prompt = $username;
    return link_to_url( url_for_private_message($username), $prompt);
}

function private_message_url($username) {
    global $Context;
    return $Context->UrlForPMToUser($username);
}

function private_message_link($username) {
    return link_to_private_message($username);
}

// -- activity hub

function link_to_activity_hub($prompt = "Activity Hub") {
    return link_to_url(url_for_activity_hub(), $prompt);
}

function url_for_activity_hub() {
    global $code_url;
    return "$code_url/activity_hub.php";
}

function redirect_to_activity_hub() {
    redirect_to_url(url_for_activity_hub());
}

// -- projectmgr

function url_for_projectmgr_show_active() {
    global $pm_url;
    return "$pm_url/projectmgr.php"
            ."?show=user_active";
}

function link_to_projectmgr_show_active() {
    return link_to_url(url_for_projectmgr_show_active(), $prompt);
}

function url_for_projectmgr_show_all() {
    global $pm_url;
    return "$pm_url/projectmgr.php"
            ."?show=user_all";
}

function link_to_projectmgr_show_all() {
    return link_to_url(url_for_projectmgr_show_all(), $prompt);
}

function link_to_projectmgr($prompt = "Manage projects") {
    return link_to_url(url_for_projectmgr(), $prompt);
}

function url_for_projectmgr() {
    global $pm_url;
    return "$pm_url/projectmgr.php";
}

function link_to_rolemgr($prompt = "Manage Roles") {
    return link_to_url(url_for_rolemgr(), $prompt);
}

function url_for_rolemgr() {
    global $admin_url;
    return "$admin_url/rolemanager.php";
}

function redirect_to_project_manager() {
    redirect_to_projectmgr();
}

function redirect_to_projectmgr() {
    redirect_to_url(url_for_projectmgr());
}

function redirect_to_pm() {
    redirect_to_project_manager();
}

// -- preview project

function url_for_preview_project($projectid) {
    global $pm_url;
    return unamp("{$pm_url}/editproject.php"
                    ."?projectid={$projectid}"
                    ."&todo=preview");
}

function redirect_to_preview_project($projectid) {
    redirect_to_url(url_for_preview_project($projectid));
}

// -- clone project

function link_to_clone_project($projectid, $prompt) {
    return link_to_url(url_for_clone_project($projectid), $prompt);
}

function url_for_clone_project($projectid) {
    global $pm_url;
    return unamp("$pm_url/editproject.php"
                ."?projectid={$projectid}"
                ."&todo=clone");
}

// -- project words

function link_to_project_words(
                    $projectid, $prompt="Words", $newtab = false) {
    return link_to_url(
                url_for_project_words($projectid), $prompt, $newtab);
}

function url_for_project_words($projectid) {
    global $wc_url;
    return "$wc_url/project_words.php"
                ."?projectid={$projectid}";
}

// -- project files

function link_to_project_files($projectid, $prompt) {
    return link_to_url(url_for_project_files($projectid), $prompt);
}

function url_for_project_files($projectid) {
    global $code_url;
    return "$code_url/filemaster.php"
                ."?projectid={$projectid}";
}

// -- project downloads

function link_to_downloads($projectid, $prompt) {
    return link_to_url(url_for_downloads($projectid), $prompt);
}

function url_for_downloads($projectid) {
    global $code_url;
    return "$code_url/downloads.php"
                ."?projectid={$projectid}";
}

// -- page detail

function url_for_page_detail($projectid) {
    global $pm_url;
    return "$pm_url/page_detail.php"
            ."?projectid={$projectid}";
}

function link_to_page_detail(
                    $projectid, $prompt, $is_new_tab = false) {
    return link_to_url(
        url_for_page_detail($projectid), $prompt, $is_new_tab);
}

// -- page detail (user)

function url_for_page_detail_user($projectid, $username = null) {
    global $pm_url;
    global $User;
    if(!$username)
        $username = $User->Username();
    return unamp("$pm_url/page_detail.php"
            ."?projectid={$projectid}"
            ."&username=$username");
}

function link_to_page_detail_user(
        $projectid, $prompt, $username = null, $is_new_tab = false) {
    return link_to_url(
        url_for_page_detail_user(
                    $projectid, $username), $prompt, $is_new_tab);
}

function url_for_page_detail_mine($projectid) {
    global $pm_url;
    return unamp("$pm_url/page_detail.php"
            ."?projectid={$projectid}"
            ."&my_pages=1");
}

function link_to_page_detail_mine($projectid, $prompt) {
    return link_to_url(
        url_for_page_detail_mine($projectid), $prompt);
}

// -----page_list


function url_for_page_list($projectid) {
    global $pm_url;
    return "$pm_url/page_list.php"
            ."?projectid={$projectid}";
}

function link_to_page_list(
                    $projectid, $prompt, $is_new_tab = false) {
    return link_to_url(
        url_for_page_list($projectid), $prompt, $is_new_tab);
}

// -- page detail (user)

function url_for_page_list_user($projectid, $username = null) {
    global $pm_url;
    global $User;
    if(!$username)
        $username = $User->Username();
    return unamp("$pm_url/page_list.php"
            ."?projectid={$projectid}"
            ."&username=$username");
}

function link_to_page_list_user(
        $projectid, $prompt, $username = null, $is_new_tab = false) {
    return link_to_url(
        url_for_page_list_user(
                    $projectid, $username), $prompt, $is_new_tab);
}

// ---------------------------------------------------


//----------------------------------------

function link_to_inbox($prompt = "Inbox") {
    return link_to_url(url_for_inbox(), $prompt);
}

function url_for_inbox() {
    global $inbox_url;
    return $inbox_url;
}

function link_to_my_projects($prompt = "My projects") {
    return link_to_url(url_for_my_projects(), $prompt);
}

function url_for_my_projects() {
    global $proof_url;
    return "$proof_url/myprojects.php";
}

function link_to_project_search($prompt = "Project search") {
    return link_to_url(url_for_project_search(), $prompt);
}

function url_for_project_search() {
    global $code_url;
    return "$code_url/search.php";
}

function link_to_stats($prompt = "Stats") {
    return link_to_url(url_for_stats(), $prompt);
}

function url_for_stats() {
    global $code_url;
    return "{$code_url}/stats/stats_central.php";
}

function link_to_logout($prompt = "Log out") {
    global $code_url;
    return link_to_url("{$code_url}/logout.php", $prompt);
}

function redirect_to_userprefs() {
    global $code_url;
    redirect_to_url(url_for_userprefs());
}

function link_to_userprefs($prompt = "Prefs") {
    return link_to_new_url(url_for_userprefs(), $prompt);
}

function url_for_userprefs() {
    global $code_url;
    return unamp("$code_url/userprefs.php");
}

// -- filemaster

function url_for_filemaster($projectid) {
    global $code_url;
    return "$code_url/filemaster.php?projectid={$projectid}";
}

function redirect_to_filemaster($projectid) {
    redirect_to_url(url_for_filemaster($projectid));
}

// -- display image

function url_for_display_image($projectid, $pagename) {
    global $pm_url;
    return unamp("$pm_url/displayimage.php"
            ."?projectid={$projectid}"
            ."&pagename={$pagename}");
}

function link_to_display_image($projectid, $pagename, $msg,
                                            $is_new_tab = false) {
    return link_to_url(
        url_for_display_image($projectid, $pagename), $msg,
                                                $is_new_tab);
}

function redirect_to_display_image($projectid, $pagename) {
    redirect_to_url(url_for_display_image($projectid, $pagename));
}

// -- wordcontext

function url_for_word_context($projectid, $mode) {
    global $wc_url;
    return unamp("$wc_url/wordcontext.php"
                ."?projectid={$projectid}"
                ."&mode={$mode}");
}

function url_for_ad_hoc_word_context($projectid) {
    return url_for_word_context($projectid, "adhoc");
}

function url_for_flagged_word_context($projectid) {
    return url_for_word_context($projectid, "aspell");
}


// -- help

function link_to_help($prompt = "Help") {
    return link_to_url( url_for_help(), $prompt);
}

function url_for_help() {
    global $code_url;
    return "$code_url/faq/faq_central.php"; 
}

// Returns a link that opens in a new window.
function new_window_link($href, $linktext) {
    global $code_url;

    $a = "<a href='$href' target='_blank'>$linktext</a>\n";
    $title = _('Link opens in a new window');
    $img = "<img src='$code_url/graphics/New-Window.gif'
        title='$title'>\n";

    return "$a $img";
}

// -------------------------------------------------------------------

function named_window_link($href, $linktext, $window_name) {
    return recycle_window_link($href, $linktext, $window_name);
}

// Returns a link that opens in a specific target.
function recycle_window_link( $href, $linktext, $target ) {
    global $code_url;

    $a = "<a href='$href' target='$target'>$linktext</a>";

    $title = _('Opens in another window');
    $img = "<img src='$code_url/graphics/Recycle-Window.gif'
        title='$title'>";

    return "$a $img";
}

// -------------------------------------------------------------------

// Returns a string containing a snippet of HTML
// for a link that opens a help topic in a new window.
function new_help_window_link( $help_topic_id, $linktext ) {
    global $code_url;

    $a = "<a href=\"newHelpWin('$help_topic_id');\">$linktext</a>";

    $title = _('Link opens in a new window');
    $img = "<img src='$code_url/graphics/New-Window.gif'
                                    title='$title'>";
    return "$a $img";
}

// -------------------------------------------------------------------

function link_to_image($projid, $pgname, $msg, $is_new_tab = true) {
    return link_to_url(
        url_for_image_server($projid, $pgname), $msg, $is_new_tab);
}

function url_for_image_server($projectid, $pagename) {
    return unamp(raw_url_for_image_server($projectid, $pagename));
}

function raw_url_for_image_server($projectid, $pagename) {
    global $code_url;
    return "$code_url/imgsrv.php"
                    ."?projectid=$projectid"
                    ."&pagename=$pagename";
}

function url_for_upload_image($projectid, $pagename) {
    global $code_url;
    return unamp("$code_url/imgsrv.php?"
        ."projectid=$projectid"
        ."&pagename=$pagename"
        ."&upload=1");
}

function link_to_upload_image(
            $projectid, $pagename, $message, $is_new_tab = true) {
    return link_to_url(
        url_for_upload_image(
            $projectid, $pagename), $message, $is_new_tab);
}

// -------------------------------------------------------------------

function upload_widget_iframe($projectid, $pagename = "") {
    global $code_url;
    return "
        <iframe id='uploadframe' name='uploadframe'
        src='".url_for_upload_widget($projectid, $pagename)."'"
                ."?projectid=$projectid"
                ."&pagename=$pagename'>
        </iframe>\n";
}

function url_for_upload_widget($projectid = "", $pagename = "") {
    global $code_url;
    return unamp("$code_url/upwidget.php")
                ."?projectid=$projectid"
                ."&pagename=$pagename'>\n";
}

/*
function link_to_upload_widget($projectid = "", $pagename = "") {
    return link_to_url(
        url_for_upload_widget($projectid, $pagename),
        $message);
}
*/

// -------------------------------------------------------------------

function redirect_to_url( $url ) {
    header( "Location: $url" );
}

function link_to_new_url($url, $msg) {
    return link_to_url($url, $msg, true);
}

function link_to_url( $url, $msg, $is_new_tab = false ) {
    return $is_new_tab
        ? "<a href='$url' target='_blank'>".h(_($msg))."</a>\n"
        : "<a href='$url'>".h(_($msg))."</a>\n";
}

// vim: sw=4 ts=4 expandtab
?>
